const jwt = require('jsonwebtoken');
const config = require('../config/config');
const UserService = require('../service/user.service');

class UserController {
  async create(ctx, next) {
    const { username, password } = ctx.request.body;
    const result = await UserService.create({ username, password });
    ctx.body = {
      code: 0,
      data: result,
    };
  }
  async login(ctx, next) {
    const { id, username } = ctx.user;
    const option = { expiresIn: '1d', algorithm: 'RS256' };
    const token = jwt.sign({ id, username }, config.APP_PRIVATE_KEY, option);
    ctx.body = {
      code: 0,
      data: token,
    };
  }
}

module.exports = new UserController();
